layui.config({
    base: basePath+'/layui/lay/modules/'
}).extend({
    eleTree:'eleTree'
}).use(['eleTree'], function() {
    layui.link(basePath+"/layui/css/eleTree.css");
    var $ = layui.jquery;
    var eleTree  = layui.eleTree;//很重要
    layer = layui.layer;
    var form = layui.form;

    eleTree.render({
        elem: '#data-list',
        url: basePath+"/organization/listOrgTreeData",
        type: "post",
        where:{token:token},
        //showCheckbox: true,
        contextmenuList: ["add" ],
        drag: true,
        accordion: false
    });
    eleTree.on("add(data-list)",function(obj) {

        var currentData = obj.currentData;
        var param = {token:token,parentId:currentData.id,name:obj.value};
        var url = basePath +"/organization/add"
        $.getJSON(url,param,function(data){

            if(data.code == 0 ){
                eleTree.reload("#data-list")
                form.val("data-form", data.data)
            }
            layer.msg(data.msg)
            return false
        })

        // 若后台修改，则重新获取后台数据，然后重载
        // eleTree.reload(".ele1", {where: {data: JSON.stringify(data.data)}})
    })
    eleTree.on("edit(data-list)",function(data) {

    })
    eleTree.on("remove(data-list)",function(obj) {
        console.log(obj)
    })
    eleTree.on("toggleSlide(data-list)",function(obj) {
        var currentData = obj.currentData;
        form.val("data-form", currentData)
    })
    eleTree.on("checkbox(data-list)",function(obj) {

    })
    eleTree.on("drag(data-list)",function(obj) {
        var currentData = obj.currentData;
        var targetData = obj.targetData
        var param = {id:currentData.id,parentId:targetData.id,token:token}
        var url = basePath +"/organization/update"
        $.getJSON(url,param,function(data){
            if(data.code == 0 ){
                eleTree.reload("#data-list")
                return true
            }else{
                layer.msg(data.msg)
                return false
            }
        })
    })


    $("#data-refresh").click(function () {
        eleTree.reload("#data-list")
    })

    form.on("submit(data-form-bt)",function(data){
        var param = data.field;
        param.token = token;
        console.log(param)
        var url = basePath +"/organization/update"
        if(param.id == null || param.id == ""){
            url = basePath +"/organization/add"
        }
        $.getJSON(url,param,function(data){
            if(data.code == 0 ){
                eleTree.reload("#data-list")
                $("button[type='reset']").click()
                form.val("data-form", { id: "",parentId:""})
            }
            layer.msg(data.msg)
            return false
        })
        return false
    })

    form.on("submit(data-form-bt-del)",function(data){
        var param = data.field;
        param.token = token;
        var url = basePath +"/organization/del"
        $.getJSON(url,param,function(data){
            if(data.code == 0 ){
                eleTree.reload("#data-list")
                $("button[type='reset']").click()
                form.val("data-form", { id: "",parentId:""})
            }
            layer.msg(data.msg)
            return false
        })
        return false
    })

});